/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import "UIConstants.js" as UIConstants

Sheet {
    id: root
    property PageItem pageItem: null
    property Button acceptButton
    property alias contentItem: contentParent.children
    property string busyText : qsTr("Busy")
    property string title: qsTr("Title")
    property bool multilineTitle: false
    property string iconSource
    property bool busy: false
    property bool titleEnabled: true

    Component.onCompleted: {
        root.acceptButton = root.getButton("acceptButton");
        root.acceptButton.enabled = false;
        root.acceptButton.platformStyle.background = "image://theme/" + UIConstants.COLOR_NAME +
                "meegotouch-sheet-button-accent"+root.acceptButton.platformStyle.__invertedString+"-background";
        root.acceptButton.platformStyle.pressedBackground = "image://theme/" + UIConstants.COLOR_NAME +
                "meegotouch-sheet-button-accent"+root.acceptButton.platformStyle.__invertedString+"-background-pressed";
        root.acceptButton.platformStyle.disabledBackground = "image://theme/" + UIConstants.COLOR_NAME +
                "meegotouch-sheet-button-accent"+root.acceptButton.platformStyle.__invertedString+"-background-disabled";
    }

    function showMessage(message) {
        infoBanner.text = message;
        infoBanner.show();
    }

    InfoBanner {
        id: infoBanner
    }

    content: Item {
        anchors.fill: parent
        Column {
            id: titleColumn            
            visible: root.titleEnabled
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.leftMargin: UIConstants.DEFAULT_MARGIN
            anchors.rightMargin: UIConstants.DEFAULT_MARGIN
            anchors.topMargin: UIConstants.DEFAULT_MARGIN
            spacing: UIConstants.DEFAULT_MARGIN
            Item {
                id: titleContents
                anchors.left: parent.left
                anchors.right: parent.right
                height: Math.max(icon.height, titleLabel.paintedHeight);
                Image {
                    id: icon
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    width: root.iconSource ? 64 : 0
                    height: root.iconSource ? 64 : 0
                    source: root.iconSource
                    visible: root.iconSource
                }
                Label {
                    id: titleLabel
                    anchors.left: icon.right
                    anchors.leftMargin: icon.visible ? UIConstants.DEFAULT_MARGIN : 0
                    anchors.right: parent.right
                    anchors.verticalCenter: parent.verticalCenter
                    font.pointSize: UIConstants.FONT_DEFAULT
                    font.family: UIConstants.FONT_FAMILY_LIGHT
                    color: UIConstants.HEADER_TITLE_COLOR
                    text: root.title

                    wrapMode: root.multilineTitle ? Text.Wrap : Text.NoWrap
                    maximumLineCount: root.multilineTitle ? 2 : 1
                    elide: root.multilineTitle ? Text.ElideRight : Text.ElideNone
                }
            }
            Separator {}
        }
        Item {
            id: contentParent
            width: parent.width
            anchors.top: root.titleEnabled ? titleColumn.bottom : parent.top
            anchors.topMargin: UIConstants.DEFAULT_MARGIN
            anchors.bottom: parent.bottom            
        }
        Column {
            id: busyItem
            visible: false
            width: parent.width
            spacing: UIConstants.DEFAULT_HALF_MARGIN
            anchors.centerIn: parent
            BusyIndicator {
                anchors.horizontalCenter: parent.horizontalCenter
                running: true
                platformStyle: BusyIndicatorStyle { size: "large" }
            }
            Label {
                anchors.horizontalCenter: parent.horizontalCenter
                text: root.busyText
                font.pointSize: UIConstants.FONT_SLARGE
                font.bold: true
            }
        }
    }

    Item {
        id: privateItem
        property Item originalParent: null
        states: [
            State {
                name: "BUSY"
                when: root.busy
                PropertyChanges {
                    target: contentParent
                    visible: false
                }
                PropertyChanges {
                    target: busyItem
                    visible: true
                }
                PropertyChanges {
                    target: root.acceptButton
                    enabled: false
                }
            }
        ]
    }
}
